/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Graphic;

/**
 *
 * @author macbook
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




import Graphic.ContainerCaratteristicsPoint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;
import scala40rules.Card;
import scala40rules.CardSet;
import scala40rules.CardSetList;


//classe CursorOverHands

public class CardsCombinations extends JPanel implements Serializable{
    /**
	 *
	 */
	private static final long serialVersionUID = 1L;

    private ArrayList<Boolean> line;
    private ArrayList<PositionArea> vectorCoordinate=new ArrayList<PositionArea>();
    private ArrayList<ThinkElement> thinkLine0;
    private ArrayList<ThinkElement> thinkLine1;
    private ArrayList<ThinkElement> thinkLine2;
    private ArrayList<ThinkElement> thinkLine3;
    private ArrayList<Punti> coordinateSeparator=new ArrayList<Punti>();
   
    static int index;
    static int index_coordinate;
    static int offsetCoordinate;
    
    private Image imgageSeparator = Toolkit.getDefaultToolkit().getImage("src/small_cards/divisore.png");

    public CardsCombinations() {

        index=0;
        index_coordinate=0;
        offsetCoordinate=0;

        setBackground(Color.decode("#DCDCDC"));

        initialPosition();

        line=new ArrayList<Boolean>();
        for(int i=0;i<4;i++){
            line.add(Boolean.TRUE);
        }

        //inizializzo coordinate0
        thinkLine0=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine0.add(new ThinkElement(i,vectorCoordinate.get(0).getPositionArea().get(i)));
            System.out.println("Posizioni area 0: "+vectorCoordinate.get(0).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(0).getPositionArea().get(i).getY());
        }

        //inizializzo coordinate1
        thinkLine1=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine1.add(new ThinkElement(i,vectorCoordinate.get(1).getPositionArea().get(i)));
            System.out.println("Posizioni area 1: "+vectorCoordinate.get(1).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(1).getPositionArea().get(i).getY());
        }

        //inizializzo coordinate2
        thinkLine2=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine2.add(new ThinkElement(i,vectorCoordinate.get(2).getPositionArea().get(i)));
            System.out.println("Posizioni area 2: "+vectorCoordinate.get(2).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(2).getPositionArea().get(i).getY());
        }

        //inizializzo coordinate3
        thinkLine3=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine3.add(new ThinkElement(i,vectorCoordinate.get(3).getPositionArea().get(i)));
            System.out.println("Posizioni area 3: "+vectorCoordinate.get(3).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(3).getPositionArea().get(i).getY());
        }

        Punti sep01=new Punti(10,125);
        Punti sep02=new Punti(10,255);
        Punti sep03=new Punti(10,385);

        
        coordinateSeparator.add(sep01);coordinateSeparator.add(sep02);coordinateSeparator.add(sep03);
        



    }
    
    

    //Disegno Componenti
    protected void paintComponent(Graphics g){
            

            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            for(int i=0;i<thinkLine0.size();i++){
                if(thinkLine0.get(i).getCard()!=null){
                    g2d.drawImage(thinkLine0.get(i).getCard().getImage(),thinkLine0.get(i).getPoint().getX(),thinkLine0.get(i).getPoint().getY(), this);
                }
                if(index>0){
                    g2d.drawImage(imgageSeparator,coordinateSeparator.get(0).getX(), coordinateSeparator.get(0).getY(), this);
                }
            }

            for(int i=0;i<thinkLine1.size();i++){
                if(thinkLine1.get(i).getCard()!=null){
                    g2d.drawImage(thinkLine1.get(i).getCard().getImage(),thinkLine1.get(i).getPoint().getX(),thinkLine1.get(i).getPoint().getY(), this);
                }
                if(index>1){
                    g2d.drawImage(imgageSeparator,coordinateSeparator.get(1).getX(), coordinateSeparator.get(1).getY(), this);
                }
            }

            for(int i=0;i<thinkLine2.size();i++){
                if(thinkLine2.get(i).getCard()!=null){
                    g2d.drawImage(thinkLine2.get(i).getCard().getImage(),thinkLine2.get(i).getPoint().getX(),thinkLine2.get(i).getPoint().getY(), this);
                }
                if(index>2){
                    g2d.drawImage(imgageSeparator,coordinateSeparator.get(2).getX(), coordinateSeparator.get(2).getY(), this);
                }
            }

            for(int i=0;i<thinkLine3.size();i++){
                if(thinkLine3.get(i).getCard()!=null){
                    g2d.drawImage(thinkLine3.get(i).getCard().getImage(),thinkLine3.get(i).getPoint().getX(),thinkLine3.get(i).getPoint().getY(), this);
                }
            }

            /*
            if(historyCards.size()!=0){
                for(int i=0;i<historyCards.size();i++){
                  g2d.drawImage(historyCards.get(i).getImage(),historyCards.get(i).getPoint().getX(),historyCards.get(i).getPoint().getY(), this);
                }
            }
            */

       /*
            if(vettoreImage.size()!=0){
                for(int i=0;i<vettoreImage.size();i++){
                    g2d.drawImage(vettoreImage.get(i),vectorCoordinate.get(index).getPositionArea().get(i).getX(),vectorCoordinate.get(index).getPositionArea().get(i).getY(), this);
                }
              
            }
       */
           
              
    }


    protected Boolean insertIntoThink(Card card){
        //int free=getLine();
        //if(free!=-1){
            switch (index) {
                case 0: 
                        if(thinkLine0.size()<=6){
                            thinkLine0=doInsert(thinkLine0,card);
                            return true;
                        }else{return false;}

                    
                case 1:
                        if(thinkLine1.size()<=6){
                            thinkLine1=doInsert(thinkLine1,card);
                            return true;
                        }else{return false;}

                    
                case 2:
                        if(thinkLine2.size()<=6){
                            thinkLine2=doInsert(thinkLine2,card);
                            return true;
                        }else{return false;}

                   
                case 3:
                        if(thinkLine3.size()<=6){
                            thinkLine3=doInsert(thinkLine3,card);
                            return true;
                        }else{return false;}

                    
                default: break;
                }
            
           
       // }
        return false;
        
    }

  /*  protected void DrawCard(int x, int y, ArrayList<PositionArea> vettore, ArrayList<Image> vettoreImage){
      this.vettoreArea=vettore;
      this.vettoreImage=vettoreImage;
      repaint();
    }
*/
    /*
    public void newLine(){
        //convalida cardset
        setLineFalse(index);
        index++;
    }

    public void deleteLine(){
        index--;
        setLineTrue(index);
    }

*/

   /* private int getLine(){
        for(int i=0;i<line.size();i++){
            if(line.get(i).equals(Boolean.TRUE)){
                return i;
            }
        }
        return -1;


    }*/

  /*  private void setLineFalse(int i){
        line.set(i, Boolean.FALSE);
    }

    private void setLineTrue(int i){
        line.set(i, Boolean.TRUE);
    }
*/
    private void initialPosition(){
        PositionArea area1=new PositionArea();
        area1.CreateAreaInRiga(5,20);

        PositionArea area2=new PositionArea();
        area2.CreateAreaInRiga(5,145);

        PositionArea area3=new PositionArea();
        area3.CreateAreaInRiga(5,275);

        PositionArea area4=new PositionArea();
        area4.CreateAreaInRiga(5,405);

        vectorCoordinate.add(area1);
        vectorCoordinate.add(area2);
        vectorCoordinate.add(area3);
        vectorCoordinate.add(area4);
    }

    public CardSet removeGroup(int indexGroup){
        CardSet returnOnHand=new CardSet();

//        deleteLine();
        

        //System.out.println("vettore: "+line.toString());
 
        //deleteLine();
        switch (indexGroup) {
                case 0:
                        for(int i=0;i<thinkLine0.size();i++){
                            returnOnHand.addCard(thinkLine0.get(i).getCard());
                            //thinkLine0=thinkLine1;
                            
                            thinkLine0.get(i).setCard(thinkLine1.get(i).getCard());
                            thinkLine0.get(i).setGroup(0);
                            thinkLine0.get(i).setPoint(vectorCoordinate.get(0).getPositionArea().get(i));
                        }
                        for(int i=0;i<thinkLine1.size();i++){
                            //thinkLine1=thinkLine2;
                            thinkLine1.get(i).setCard(thinkLine2.get(i).getCard());
                            thinkLine1.get(i).setGroup(1);
                            thinkLine1.get(i).setPoint(vectorCoordinate.get(1).getPositionArea().get(i));
                        }
                        for(int i=0;i<thinkLine1.size();i++){
                            //thinkLine2=thinkLine3;
                            thinkLine2.get(i).setCard(thinkLine3.get(i).getCard());
                            thinkLine2.get(i).setGroup(2);
                            thinkLine2.get(i).setPoint(vectorCoordinate.get(2).getPositionArea().get(i));
                        }

                        

                        System.out.println("INDEX: "+index);



                        repaint();
                        return returnOnHand;

                case 1: 
                        for(int i=0;i<thinkLine1.size();i++){
                            returnOnHand.addCard(thinkLine1.get(i).getCard());
                            thinkLine1.get(i).setCard(thinkLine2.get(i).getCard());
                            thinkLine1.get(i).setGroup(1);
                            thinkLine1.get(i).setPoint(vectorCoordinate.get(1).getPositionArea().get(i));
                        }
                        for(int i=0;i<thinkLine1.size();i++){
                            //thinkLine2=thinkLine3;
                            thinkLine2.get(i).setCard(thinkLine3.get(i).getCard());
                            thinkLine2.get(i).setGroup(2);
                            thinkLine2.get(i).setPoint(vectorCoordinate.get(2).getPositionArea().get(i));
                        }

                        

                        repaint();
                        return returnOnHand;

                case 2: 
                        for(int i=0;i<thinkLine2.size();i++){
                            returnOnHand.addCard(thinkLine2.get(i).getCard());
                            thinkLine2.get(i).setCard(thinkLine3.get(i).getCard());
                            thinkLine2.get(i).setGroup(2);
                            thinkLine2.get(i).setPoint(vectorCoordinate.get(2).getPositionArea().get(i));
                            thinkLine3.get(i).setCard(null);
                        }

                        
                        
                        repaint();
                        return returnOnHand;

                case 3: 
                        for(int i=0;i<thinkLine3.size();i++){
                            returnOnHand.addCard(thinkLine3.get(i).getCard());
                            thinkLine3.get(i).setCard(null);
                        }

                       

                        repaint();
                        return returnOnHand;

                default: break;
                }

        return returnOnHand;

    }




    private ArrayList<ThinkElement> doInsert(ArrayList<ThinkElement> think,Card card){
       System.out.println("atro QUI");
        if(checkFree(think)){
           System.out.println("QUI");
        if(think.size()<=6){
          for(int i=0;i<think.size();i++){
            if(think.get(i).getCard()==null){
                think.get(i).setCard(card);
                System.out.println("settata:  "+think.get(i).getCard().getSymbol());
                break;
            }
         }
        }
         
         
         repaint();
         }
         return think;


   }

    private boolean checkFree(ArrayList<ThinkElement> think){
        for(int i=0;i<think.size();i++){
            try{
                if(think.get(i).getCard().getImage()!=null){
                return true;
            }
            }catch(Exception e){
                return true;
            }

        }
        return false;
    }

    public int dimList(){
      /*  for(int i=0;i<4;i++){
            if(line.get(i).equals(Boolean.TRUE)){
                return i;
            }
        }
        return -1;
    }*/
    return index;
    }


    public void removeAll(){

        thinkLine0=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine0.add(new ThinkElement(i,vectorCoordinate.get(0).getPositionArea().get(i)));
            System.out.println("Posizioni area 0: "+vectorCoordinate.get(0).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(0).getPositionArea().get(i).getY());
        }

        thinkLine1=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine1.add(new ThinkElement(i,vectorCoordinate.get(1).getPositionArea().get(i)));
            System.out.println("Posizioni area 1: "+vectorCoordinate.get(1).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(1).getPositionArea().get(i).getY());
        }

        //inizializzo coordinate2
        thinkLine2=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine2.add(new ThinkElement(i,vectorCoordinate.get(2).getPositionArea().get(i)));
            System.out.println("Posizioni area 2: "+vectorCoordinate.get(2).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(2).getPositionArea().get(i).getY());
        }

        //inizializzo coordinate3
        thinkLine3=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine3.add(new ThinkElement(i,vectorCoordinate.get(3).getPositionArea().get(i)));
            System.out.println("Posizioni area 3: "+vectorCoordinate.get(3).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(3).getPositionArea().get(i).getY());
        }


        //riporto l'indice di posizionamento iniziale alla riga 0
        index=0;
        repaint();

    }



    public void removeWithoutLast(CardSet c){

        thinkLine0=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine0.add(new ThinkElement(i,vectorCoordinate.get(0).getPositionArea().get(i)));
            System.out.println("Posizioni area 0: "+vectorCoordinate.get(0).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(0).getPositionArea().get(i).getY());
        }

        thinkLine1=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine1.add(new ThinkElement(i,vectorCoordinate.get(1).getPositionArea().get(i)));
            System.out.println("Posizioni area 1: "+vectorCoordinate.get(1).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(1).getPositionArea().get(i).getY());
        }

        //inizializzo coordinate2
        thinkLine2=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine2.add(new ThinkElement(i,vectorCoordinate.get(2).getPositionArea().get(i)));
            System.out.println("Posizioni area 2: "+vectorCoordinate.get(2).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(2).getPositionArea().get(i).getY());
        }

        //inizializzo coordinate3
        thinkLine3=new ArrayList<ThinkElement>();
        for(int i=0;i<6;i++){
            thinkLine3.add(new ThinkElement(i,vectorCoordinate.get(3).getPositionArea().get(i)));
            System.out.println("Posizioni area 3: "+vectorCoordinate.get(3).getPositionArea().get(i).getX()+" | "+vectorCoordinate.get(3).getPositionArea().get(i).getY());
        }

        for(int i=0;i<c.size();i++){
            insertIntoThink(c.getCard(i));
        }
        

        repaint();

    }

    public void increaseIndexLine(){
        index++;
        repaint();


    }

    public void decreaseIndexLine(){
        index--;
        repaint();
    }


    public void refreshLine(int position, CardSet c){
        switch(position){
           case 0: 
                    for(int i=0;i<c.size();i++){
                        thinkLine0.get(i).setCard(c.getCard(i));
                    }

                    break;
                    
           case 1:

                    for(int i=0;i<c.size();i++){
                        thinkLine1.get(i).setCard(c.getCard(i));
                    }
                    break;
                
           case 2:

                    for(int i=0;i<c.size();i++){
                        thinkLine2.get(i).setCard(c.getCard(i));
                    }
                    break;
               
               
           case 3:
                
                    for(int i=0;i<c.size();i++){
                        thinkLine3.get(i).setCard(c.getCard(i));
                    }
                    break;
        }
        repaint(); 

}

}

